package cn.xmy.demo01.flier;


import cn.xmy.demo01.pojo.GEUser;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
@WebFilter({"/QueryPostDetail", "/PersonalCenterServlet"})
public class GFliter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        HttpServletResponse response=(HttpServletResponse) servletResponse;
        HttpSession session=request.getSession();
        GEUser geUser=(GEUser) session.getAttribute("GUser");
        if(geUser==null){
            String path=request.getServletPath();
            if(path.equals("/home.jsp")){
                filterChain.doFilter(request,response);
            }else {
                request.getRequestDispatcher("guserRegistger.jsp").forward(request,response);
            }

        }else {
            filterChain.doFilter(request,response);
        }



    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
